package cf.luobo.covid_19.action;

import cf.luobo.covid_19.javaBean.pojo.ErrMsg;
import cf.luobo.covid_19.javaBean.pojo.Province;
import cf.luobo.covid_19.service.ProvinceService;
import cf.luobo.covid_19.service.imp.ProvinceServiceImp;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/province")
public class ProvinceServlet extends HttpServlet {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private HttpServletRequest request;
    private HttpServletResponse response;
    private ProvinceService service = new ProvinceServiceImp();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.request = req;
        this.response = resp;

        String method = request.getParameter("method"); //获取一个请求的数据
        if (method.equals("queryProvinceJson")) {
            try {
                this.queryProvinceJson();
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (method.equals("queryNowProvinceJson")) {
            this.queryNowProvinceJson();
        } else if (method.equals("queryPro")) {
            this.queryPro();
        } else if (method.equals("updatePro")) {
            this.updatePro();
        }
    }

    /**
     * 查询指定日期的所有省份数据
     */
    public void queryProvinceJson() throws IOException, ParseException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        String date_in = request.getParameter("date");
        PrintWriter out = response.getWriter();
        Date date = sdf.parse(date_in);
        List<Province> list = service.allList(new java.sql.Date(date.getTime()));
        String text = JSON.toJSONString(list);
        out.print(text); // 输出
        out.flush();
    }

    /**
     * 查询当前日期的所有省份数据
     */
    public void queryNowProvinceJson() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        List<Province> list = service.allList(new java.sql.Date(System.currentTimeMillis()));
        String text = JSON.toJSONString(list);
        out.print(text); // 输出
        out.flush();
    }

    /**
     * 查询单个省份的数据
     */
    public void queryPro() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        String date_in = request.getParameter("date");
        String name_in = request.getParameter("name");
        Date date = null;
        try {
            date = sdf.parse(date_in);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Province province = service.selectPro(name_in, new java.sql.Date(date.getTime()));
        String text = JSON.toJSONString(province);
        out.print(text); // 输出
        out.flush();
    }

    /**
     * 修改指定日期的省份数据
     */
    public void updatePro() throws IOException {
        response.setContentType("text/json; charset=utf-8");    // 设置response的编码及格式
        PrintWriter out = response.getWriter();
        String chspro = request.getParameter("province");
        String engpro = Province.engToChs.getKey(chspro);
        int currentConfirmed = Integer.parseInt(request.getParameter("currentConfirmed"));
        int suspected = Integer.parseInt(request.getParameter("suspected"));
        int cured = Integer.parseInt(request.getParameter("cured"));
        int dead = Integer.parseInt(request.getParameter("dead"));
        String date_in = request.getParameter("date");
        Date date = null;
        try {
            date = sdf.parse(date_in);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Province province = new Province(engpro, currentConfirmed, cured, dead, suspected);
        ErrMsg errmsg = service.updatePro(province, new java.sql.Date(date.getTime()));
        String text = JSON.toJSONString(errmsg);
        out.print(text); // 输出
        out.flush();
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
